package com.eve.util;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: ivan.yao<haif.yao@gmail.com>
 * @date: 16/5/16.
 */
public class RegexpHelper {

    private static String separator = ",";

    /**
     * 通配符匹配
     *
     * @param str
     * @param patternstr
     * @return
     */
    public final static boolean isGlobmatches(String str, String patternstr) {
        if (StringUtils.isBlank(patternstr)) {
            return false;
        }
        if (patternstr.indexOf(separator) == -1) {
            Pattern pattern = Pattern.compile(patternstr);
            Matcher matcher = pattern.matcher(str);
            return matcher.matches();
        }else{
            String[] patterns = patternstr.split(separator);
            for (String patternStr:patterns) {
                Pattern pattern = Pattern.compile(patternStr);
                Matcher matcher = pattern.matcher(str);
                if( matcher.matches()){
                    return true;
                }
            }
            return false;
        }
    }
}
